<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="dxlateplate.LatePlate" %>
<%@ page import="dxlateplate.PMF" %>

<html>
<head>
    <link rel="icon" type="image/png" href="/favicon.png">
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
    <title>Delta Chi Late Plates</title>
</head>

<script type="text/javascript">

function ErrorCheck(user, loginURL) {
    var name = document.latePlateForm.name.value;
    var day = "";
    var type = "";

    //check permanent eligibility
    if(document.latePlateForm.permanent.checked && !user) {
        alert("You must login to request a permanent late plate.");
        return false;
    }

    //Get day
    for(var i = 0; i < document.latePlateForm.day.length; i++) {
        if(document.latePlateForm.day[i].checked) {
            day = document.latePlateForm.day[i].value;
        }
    }

    //Get meal
    for(var i = 0; i < document.latePlateForm.type.length; i++) {
        if(document.latePlateForm.type[i].checked) {
            type = document.latePlateForm.type[i].value;
        }
    }

    //Login
    if(!user) {
        if(confirm("You forgot to login. \n\nDo you wish to login in now? \nYou will need to resubmit your request upon logging in.")) {
            window.location = loginURL;
            return false;
        }
    }

    //Cannot have blank name.
    if(name == "") {
        alert("Please enter your name.");
        return false;
    }

    //Friday dinner not available
    if((day == "Friday") && (type == "2")) {
        alert("Sorry, but there is no Friday dinner.");
        return false;
    }

    return true;
}

</script>
<body class="home">
    <div id="crest">
        <img alt="crest" src="images/crest.png" height="85" width="50">
        <a href="latePlateHome.jsp"><img alt="Late Plates" src="images/late_plate_title.png"></a>
        <img alt="crest" src="images/crest.png" height="85" width="50">
    </div>

    <br>

    <div id="form">
    	<p>Download the updated <a href="http://dl.dropbox.com/u/25263530/LatePlate.apk"><b>Android App</b></a>!</p>
    	
        <%
            boolean logged = false;
            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();
            String loginURL = userService.createLoginURL(request.getRequestURI());
            if (user != null) {
                logged = true;
        %>
        <p>Hello, <%= user.getNickname() %>.
        <br>Review the menu or make a request!</p>
        <div align=center>
        <table>
        	<tr>
        		<td align="center" padding="0px">
        			<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>"><img src="images/signout_button.png" width=50% height=50% onmouseover="this.src='images/signout_button_pushed.png'" onmouseout="this.src='images/signout_button.png'"></img></a>
        		</td>
        		<td align="center">
        			<a href="latePlateList.jsp"><img src="images/list_button.png" width=50% height=50% onmouseover="this.src='images/list_button_pushed.png'" onmouseout="this.src='images/list_button.png'"></img></a>
        		</td>
        	</tr>
        </table>
        </div><br>
        <%
            } else {
        %>
        <p>Hello! Sign in to be able to manage your requests.</p>
        <div align=center>
        <table>
        	<tr>
        		<td align="center" padding="0px">
        			<a href="<%=loginURL%>"><img src="images/signin_button.png" width=50% height=50% onmouseover="this.src='images/signin_button_pushed.png'" onmouseout="this.src='images/signin_button.png'"></img></a>
        		</td>
        		<td align="center">
        			<a href="latePlateList.jsp"><img src="images/list_button.png" width=50% height=50% onmouseover="this.src='images/list_button_pushed.png'" onmouseout="this.src='images/list_button.png'"></img></a>
        		</td>
        	</tr>
        </table>
        </div><br>
        <%
            }
        %>

        <a href="http://dl.dropbox.com/u/25263530/menu.png"><img src="http://dl.dropbox.com/u/25263530/menu.png" border="2px solid black" width="520" height="220"></img></a>

        <br>

        <form action="/submitLatePlate" name="latePlateForm" method="post" onSubmit="return ErrorCheck(<%= logged %>, '<%= loginURL %>')">
            <div>
                <i>Enter your name:</i><br>
                <textarea name="name" rows="1" cols="30"></textarea>
            </div>
            <br>

            <i>Choose which meal:</i><br>
            <div align="center">
                <table id="mealSelect">
                    <tr>
                        <td width="150">
                            <input type="radio" name="day" value="Monday" checked>Monday</input><br>
                            <input type="radio" name="day" value="Tuesday">Tuesday</input><br>
                            <input type="radio" name="day" value="Wednesday">Wednesday</input><br>
                            <input type="radio" name="day" value="Thursday">Thursday</input><br>
                            <input type="radio" name="day" value="Friday">Friday</input><br>
                        </td>
                        <td>
                            <input type="radio" name="type" value="1" checked> Lunch <br>
                            <input type="radio" name="type" value="2"> Dinner<br>
                        </td>
                    </tr>
                </table>
            </div>
            <br>
            <div>
                <i>Enter your comments:</i><br>
                <textarea name="comments" rows="3" cols="60"></textarea>
            </div><br>
            <div><input type="checkbox" name="permanent">Make Permanent</input></div><br>
            <div><input type="submit" value="Request Late Plate" /></div>
        </form>
        <p>&copy Travis Wooten 2011-2012</p>
    </div>
  </body>
</html>
